package com.markus.code.剑指offer;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/8 12:32
 * todo: 把数字翻译成字符串
 */
public class TranslateNum {
    /**
     * 这道题，我们可以用动态规划来做
     * 状态：dp[i] 表示以i位结尾的前缀串的方案数
     * 1、考虑当前字符
     * 2、考虑当前字符和前一个字符的组合情况
     */
    public int translateNum(int num) {
        String str = String.valueOf(num);
        int[] dp = new int[str.length()];
        dp[0] = 1;
        String sub = str.substring(0, 2);
        dp[1] = compare(sub)?2:1;
        for (int i=2;i<str.length();i++){
            dp[i] = dp[i-1];
            sub = str.substring(i-1,i+1);
            if (compare(sub)){
                dp[i] += dp[i-2];
            }
        }
        return dp[dp.length-1];
    }

    private boolean compare(String sub) {
        int num = Integer.parseInt(sub);
        return num>=10 && num <= 25;
    }

    public static void main(String[] args) {
        int num = 12258;
        TranslateNum demo = new TranslateNum();
        System.out.println(demo.translateNum(num));
    }
}
